/*
 * Configuration.java
 *
 * Created on Aug 22, 2009, 7:15:15 PM
 */
package br.com.fieo.sgee.view.owner;

import br.com.fieo.sgee.exception.ValidFieldException;
import br.com.fieo.sgee.logic.UserLogic;
import java.awt.Color;
import javax.swing.BorderFactory;
import br.com.fieo.sgee.logic.EstimateLogic;
import br.com.fieo.sgee.logic.KwhLogic;
import br.com.fieo.sgee.model.Estimate;
import br.com.fieo.sgee.model.Kwh;

/**
 *
 * @author rbueno
 */
public class Configuration extends javax.swing.JPanel {

    /** ultima estimativa armazenada */
    private Estimate estimate;
    /** ultimo kwt armazenado */
    private Kwh kwh;
    /** Mensagem de confirmação quando alteramos a senha */
    private static final String PASSWORD_CHANGE_SUCCESS = "Senha alterada com sucesso!";
    private static final String ESTIMATE_SUCCESS = "Estimativa alterada com sucesso!";
    private static final String KWH_SUCCESS = "Kwh alterado com sucesso!";

    /** Creates new form Configuration */
    public Configuration() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jTextPane1 = new javax.swing.JTextPane();
        jTextFieldEstimativa = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButtonEstimativa1 = new javax.swing.JButton();
        jLabelTitle = new javax.swing.JLabel();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jTextPane3 = new javax.swing.JTextPane();
        jTextFieldKwh1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButtonEstimativa2 = new javax.swing.JButton();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jTextPane2 = new javax.swing.JTextPane();
        jPasswordFieldConfirmation = new javax.swing.JPasswordField();
        jPasswordField = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButtonPassword = new javax.swing.JButton();
        jTextPaneMessage = new javax.swing.JTextPane();

        setBackground(new java.awt.Color(255, 255, 255));
        setForeground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(1024, 724));

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Valor Estimativa", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 14))); // NOI18N
        jLayeredPane1.setPreferredSize(new java.awt.Dimension(341, 100));

        jTextPane1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jTextPane1.setForeground(new java.awt.Color(0, 0, 153));
        jTextPane1.setText("Informar a quantidade que deseja gastar de energia elétrica nesse mês, para controlar os seus gastos.");
        jTextPane1.setBounds(10, 30, 320, 70);
        jLayeredPane1.add(jTextPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        estimate = EstimateLogic.getInstance().loadLast();
        jTextFieldEstimativa.setText(Float.toString(estimate.getValue()));
        jTextFieldEstimativa.setMaximumSize(new java.awt.Dimension(14, 28));
        jTextFieldEstimativa.setBounds(90, 130, 170, 30);
        jLayeredPane1.add(jTextFieldEstimativa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel1.setText("R$");
        jLabel1.setBounds(60, 130, 20, 30);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButtonEstimativa1.setText("Salvar Estimativa");
        jButtonEstimativa1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEstimativa1ActionPerformed(evt);
            }
        });
        jButtonEstimativa1.setBounds(110, 170, 150, 30);
        jLayeredPane1.add(jButtonEstimativa1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabelTitle.setFont(new java.awt.Font("Times New Roman", 1, 36));
        jLabelTitle.setForeground(new java.awt.Color(0, 51, 153));
        jLabelTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelTitle.setText("Configuração do Sistema");

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Valor Kwh", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 14))); // NOI18N
        jLayeredPane2.setPreferredSize(new java.awt.Dimension(341, 100));

        jTextPane3.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jTextPane3.setForeground(new java.awt.Color(0, 0, 153));
        jTextPane3.setText("Informar o valor gasto por kwh nesse mês. Verificar a conta de energia do mês passado.");
        jTextPane3.setBounds(10, 30, 320, 70);
        jLayeredPane2.add(jTextPane3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextFieldKwh1.setMaximumSize(new java.awt.Dimension(14, 28));
        kwh = KwhLogic.getInstance().loadLast();
        jTextFieldKwh1.setText(Float.toString(kwh.getValue()));
        jTextFieldKwh1.setBounds(100, 130, 170, 30);
        jLayeredPane2.add(jTextFieldKwh1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel3.setText("R$");
        jLabel3.setBounds(70, 130, 20, 30);
        jLayeredPane2.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButtonEstimativa2.setText("Salvar Kwh");
        jButtonEstimativa2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEstimativa2ActionPerformed(evt);
            }
        });
        jButtonEstimativa2.setBounds(150, 170, 120, 30);
        jLayeredPane2.add(jButtonEstimativa2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Senha de Acesso", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 14))); // NOI18N
        jLayeredPane3.setPreferredSize(new java.awt.Dimension(342, 100));

        jTextPane2.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jTextPane2.setForeground(new java.awt.Color(0, 0, 153));
        jTextPane2.setText("Preencha para alterar a sua senha.");
        jTextPane2.setBounds(10, 30, 320, 70);
        jLayeredPane3.add(jTextPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPasswordFieldConfirmation.setMaximumSize(new java.awt.Dimension(14, 28));
        jPasswordFieldConfirmation.setBounds(130, 150, 170, 30);
        jLayeredPane3.add(jPasswordFieldConfirmation, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPasswordField.setMaximumSize(new java.awt.Dimension(14, 28));
        jPasswordField.setBounds(130, 110, 170, 30);
        jLayeredPane3.add(jPasswordField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel2.setText("Confirmação");
        jLabel2.setBounds(40, 150, 80, 30);
        jLayeredPane3.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel4.setText("Senha");
        jLabel4.setBounds(80, 110, 40, 30);
        jLayeredPane3.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButtonPassword.setText("Salvar Senha");
        jButtonPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPasswordActionPerformed(evt);
            }
        });
        jButtonPassword.setBounds(180, 190, 120, 30);
        jLayeredPane3.add(jButtonPassword, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextPaneMessage.setEditable(false);
        jTextPaneMessage.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jTextPaneMessage.setForeground(new java.awt.Color(221, 0, 0));
        jTextPaneMessage.setDisabledTextColor(new java.awt.Color(221, 0, 0));
        jTextPaneMessage.setEnabled(false);
        jTextPaneMessage.setMargin(new java.awt.Insets(3, 160, 3, 60));
        jTextPaneMessage.setMaximumSize(new java.awt.Dimension(160, 27));
        jTextPaneMessage.setOpaque(false);
        jTextPaneMessage.setRequestFocusEnabled(false);
        jTextPaneMessage.setSelectedTextColor(null);
        jTextPaneMessage.setSelectionColor(null);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(312, 312, 312)
                .add(jLabelTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 395, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(317, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLayeredPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(40, 40, 40))
            .add(layout.createSequentialGroup()
                .add(157, 157, 157)
                .add(jTextPaneMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 732, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabelTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(54, 54, 54)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jLayeredPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 274, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(26, 26, 26)
                .add(jTextPaneMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(203, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEstimativa1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEstimativa1ActionPerformed

        try {

            estimate.setValue(Float.parseFloat(jTextFieldEstimativa.getText()));
            EstimateLogic.getInstance().merge(estimate);

            jTextFieldEstimativa.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            jTextPaneMessage.setText(ESTIMATE_SUCCESS);
            jTextPaneMessage.setDisabledTextColor(Color.GREEN);
            jTextPaneMessage.setVisible(true);

        } catch (ValidFieldException ex) {

            jTextPaneMessage.setVisible(true);
            jTextPaneMessage.setText(ex.getMessage());
            jTextPaneMessage.setDisabledTextColor(Color.RED);
            jTextFieldEstimativa.setBorder(BorderFactory.createLineBorder(Color.RED));

        } catch (NumberFormatException ex) {

            jTextPaneMessage.setVisible(true);
            jTextPaneMessage.setText(ValidFieldException.ONLY_NUMBERS);
            jTextPaneMessage.setDisabledTextColor(Color.RED);
            jTextFieldEstimativa.setBorder(BorderFactory.createLineBorder(Color.RED));

        }

    }//GEN-LAST:event_jButtonEstimativa1ActionPerformed

    private void jButtonEstimativa2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEstimativa2ActionPerformed

        try {

            kwh.setValue(Float.parseFloat(jTextFieldKwh1.getText()));
            KwhLogic.getInstance().merge(kwh);

            jTextFieldKwh1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            jTextPaneMessage.setText(KWH_SUCCESS);
            jTextPaneMessage.setDisabledTextColor(Color.GREEN);
            jTextPaneMessage.setVisible(true);

        } catch (ValidFieldException ex) {

            jTextPaneMessage.setVisible(true);
            jTextPaneMessage.setText(ex.getMessage());
            jTextPaneMessage.setDisabledTextColor(Color.RED);
            jTextFieldKwh1.setBorder(BorderFactory.createLineBorder(Color.RED));

        } catch (NumberFormatException ex) {

            jTextPaneMessage.setVisible(true);
            jTextPaneMessage.setText(ValidFieldException.ONLY_NUMBERS);
            jTextPaneMessage.setDisabledTextColor(Color.RED);
            jTextFieldKwh1.setBorder(BorderFactory.createLineBorder(Color.RED));

        }

    }//GEN-LAST:event_jButtonEstimativa2ActionPerformed

    /**
     * Utilizado para alterar a senha do usuário atual
     * @param evt
     */
    private void jButtonPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPasswordActionPerformed

        try {

            UserLogic.getInstance().changePassword(jPasswordField.getPassword(), jPasswordFieldConfirmation.getPassword());

            jPasswordFieldConfirmation.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            jPasswordField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            jTextPaneMessage.setText(PASSWORD_CHANGE_SUCCESS);
            jTextPaneMessage.setDisabledTextColor(Color.GREEN);
            jTextPaneMessage.setVisible(true);

        } catch (ValidFieldException ex) {

            jTextPaneMessage.setVisible(true);
            jTextPaneMessage.setDisabledTextColor(Color.RED);

            StringBuffer mensagem = new StringBuffer();
            for (String s : ex.getMessages().keySet()) {
                String value = ex.getMessages().get(s);
                mensagem.append(s.toString() + ": " + value.toString() + System.getProperty("line.separator"));
                if (s.toString().equals("confirmação")) {
                    jPasswordFieldConfirmation.setBorder(BorderFactory.createLineBorder(Color.RED));
                }
                if (s.toString().equals("senha")) {
                    jPasswordField.setBorder(BorderFactory.createLineBorder(Color.RED));
                }
            }

            jTextPaneMessage.setText(mensagem.toString());

        }

    }//GEN-LAST:event_jButtonPasswordActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEstimativa1;
    private javax.swing.JButton jButtonEstimativa2;
    private javax.swing.JButton jButtonPassword;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelTitle;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JPasswordField jPasswordFieldConfirmation;
    private javax.swing.JTextField jTextFieldEstimativa;
    private javax.swing.JTextField jTextFieldKwh1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane2;
    private javax.swing.JTextPane jTextPane3;
    private javax.swing.JTextPane jTextPaneMessage;
    // End of variables declaration//GEN-END:variables
}
